﻿using DotNetCore.CAP;
using Microsoft.AspNetCore.Mvc;
using MQCommon;
using MQCommon.Model;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace UserServices.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CreateUserController : ControllerBase
    {
        private readonly ICapPublisher capPublisher;
        public CreateUserController(ICapPublisher capPublisher)
        {
            this.capPublisher = capPublisher;
        }
        // GET: api/<CreateUserController>
        [Route("ctreatUser")]
        [NonAction]
        [CapSubscribe("order.ctreatUser")]//用于接收mq消息
        public ActionResult<UserModel> ctreatUser(UserModel um, [FromCap] CapHeader header)
        {
            var token = header["token"];//基础在cap的header，常用于授权等信息传递
            DapperHelper dh = new DapperHelper();
            Order od=new Order();
            od.Id = um.Id;
            od.ctreattime=DateTime.Now;
            od.Title = "下单";
            od.userid = um.Id;
            IDictionary<string, string> dicHeader = new Dictionary<string, string>();
            dicHeader.Add("token", "123");
           
            dh.BeginTransaction(() => {
                dh.Execute($"insert into user values({um.Id},'{um.Name}','{um.des}','{um.ctreattime}','{token}')");
                capPublisher.Publish<Order>("order.createorder", od, dicHeader);//发布mq消息
            }, capPublisher);
            return Content("OK");
        }

        
    }
}
